/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.bubble;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JToolBar;
import javax.swing.ListCellRenderer;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

import twaver.Element;
import twaver.EnumType;
import twaver.EnumTypeManager;
import twaver.Node;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.chart.Bubble;
import twaver.chart.BubbleChart;
import demo.chart.Portlet;

public class HashBubbleChart extends Portlet {
	private boolean yFlag = true;
	private TDataBox box = new TDataBox();
	private int Style = TWaverConst.GRADIENT_RADIAL_NW;
	private BubbleChart chart = new BubbleChart(box) {
		protected String getValueText(Element element, Bubble bubble, int index) {
			return bubble.getText();
		}

		protected Color getShapeColor(Element element,Bubble bubble,int index) {
			if (Math.abs(bubble.getX() - chart.getYAxisStartPosition()) < 5) {
				return chart.getYAxisOutlineColor();
			}
			if (bubble.getText().equals("A")) {
				return new Color(138, 181, 197);
			}
			else if (bubble.getText().equals("B")) {
				return new Color(160, 184, 129);
			}
			else if (bubble.getText().equals("C")) {
				return new Color(255, 153, 0);
			}
			else if (bubble.getText().equals("D")) {
				return Color.RED.brighter();
			}
			else if (bubble.getText().equals("E")) {
				return Color.CYAN;
			}
			return super.getShapeColor(element, bubble, index);
		}

		protected double getShapeSize(Element element,Bubble bubble, int index) {
			return super.getShapeSize(element, bubble,index) / 300d * Math.min(this.getDrawBound().width, this.getDrawBound().height);
		}
	};

	public HashBubbleChart() {
		super.initialize(chart);
		chart.setShadowOffset(0);
		chart.setXGap(20);
		chart.setYGap(20);
		chart.setXScaleLineVisible(false);
		chart.setXAxisVisible(false);
		chart.setXAxisOutlineColor(Color.WHITE);
		chart.setXScaleTextVisible(false);
		chart.setYScaleLineVisible(false);
		chart.setYAxisVisible(true);
		chart.setYScaleTextVisible(false);
		chart.setYScaleMaxValue(100);
		chart.setYScaleMinValue(0);
		chart.setYScaleValueGap(10);
		chart.setBubbleGradientFactory(Style);
		chart.setBubbleAlpha(0.9f);
		chart.setBackgroundVisible(true);
		chart.setBackgroundGradient(false);
		chart.getLegendPane().setVisible(false);

		Element element = new Node();
		box.addElement(element);
		initBubble(element, "A", 30, 50, 90);
		initBubble(element, "B", 50, 60, 80);
		initBubble(element, "C", 70, 60, 70);
		initBubble(element, "D", 60, 40, 70);
		initBubble(element, "E", 65, 20, 50);
	}

	public void initBubble(Element element, String name, double x, double y, double value) {
		Bubble bubble = new Bubble(name, x, y, value);
		element.addChartBubble(bubble);
	}

	public JToolBar getControlPanel() {
		JToolBar toolbar = super.getControlPanel();
		toolbar.add(getRunButton());
		final JComboBox combobox = new JComboBox();
		final BasicComboBoxRenderer renderer = new BasicComboBoxRenderer();
		TWaverUtil.setHorizontalAlignment(renderer, TUIManager.getString(TWaverConst.TABLE_ALIGNMENT_ENUMTYPE));
		combobox.setRenderer(new ListCellRenderer() {
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

				String text = null;
				Icon icon = null;
				Color background = null;
				Color foreground = null;
				if (value instanceof EnumType) {
					EnumType enumType = (EnumType) value;
					text = enumType.toString();
					icon = enumType.getIcon();
					background = enumType.getBackground();
					foreground = enumType.getForeground();
					if (background != null && isSelected) {
						background = background.darker();
					}
				}
				renderer.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
				renderer.setIcon(icon);
				renderer.setToolTipText(text);
				if (foreground != null) {
					renderer.setForeground(foreground);
				}
				if (background != null) {
					renderer.setBackground(background);
				}
				return renderer;
			}
		});
		combobox.setPreferredSize(new Dimension(combobox.getPreferredSize().width, 20));
		final Object[] gradientTypes = EnumTypeManager.getInstance().getEnumTypes(TWaverConst.ENUM_GRADIENT);
		final int length = gradientTypes.length;
		for (int i = 0; i < length; i++) {
			EnumType enumeration = (EnumType) gradientTypes[i];
			combobox.addItem(enumeration);
			if (Style == ((Integer) (enumeration.getValue())).intValue()) {
				combobox.setSelectedIndex(i);
			}
		}
		combobox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				Object object = combobox.getSelectedItem();
				for (int i = 0; i < length; i++) {
					EnumType enumeration = (EnumType) gradientTypes[i];
					if (enumeration == object) {
						chart.setBubbleGradientFactory(((Integer) (enumeration.getValue())).intValue());
					}
				}
			}
		});
		JLabel label = new JLabel("Gradient:");
		toolbar.add(label);
		toolbar.add(combobox);
		return toolbar;
	}

	public void run() {
		if (!isRunning()) {
			return;
		}
		double yValue = chart.getYAxisStartPosition();
		double yScaleRange = chart.getYScaleMaxValue() - chart.getYScaleMinValue();
		int step = 50;
		if (yFlag) {
			yValue += yScaleRange / step;
		} else {
			yValue -= (yScaleRange) / step;
		}
		if (yValue > chart.getYScaleMaxValue()) {
			yFlag = false;
			yValue = chart.getYScaleMaxValue();
		}
		if (yValue < chart.getYScaleMinValue()) {
			yFlag = true;
			yValue = chart.getYScaleMinValue();
		}
		chart.setYAxisOutlineColor(TWaverUtil.getRandomColor());
		chart.setYAxisStartPosition(yValue);
	}
}